home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 June: Reference Library / Dev.CD Jun 99 RL Disk 1.toast / Technical Documentation / Macintosh Technotes and Q&As / technotes / tn / Apple_Guide_Variables / Source / AGHelper.c next >
Encoding:
C/C++ Source or Header  |  1997-12-10  |  5.9 KB  |  195 lines  |  [TEXT/R*ch]

  1. /*
  2.  *  cSmallDaemon
  3.  *
  4.  *  7/92 Greg Robbins         based on code by C.K. Haun
  5.  *
  6.  *  8/96 Modified by Don Swatman
  7.  *
  8.  *  This is a minimal faceless background application for System 7.
  9.  *
  10.  *  It demonstrates how to install and dispatch Apple events, as well
  11.  *  as the other bare essentials for a faceless background app.
  12.  *
  13.  *  The file type for this application should be 'APPL' if it will be launched
  14.  *  like an application or 'appe' if it will be placed into the Extensions
  15.  *  folder and launched at startup.  'appe' files can also have an INIT resource 
  16.  *  to put up an icon (using ShowInit) at startup.
  17.  */
  18.  
  19. //===============================================================================
  20. //  AGHelper
  21. //
  22. // This app has been modified to compile under the latest enviroments. It also
  23. // has had the AGValues unit added to it. This adds context check handlers and
  24. // apple events to allow apple guides to track what sequences have been
  25. // displayed. This turns it into an Apple Guide helper app. There are two calls
  26. // into the unit. 
  27. //      OSErr InitAGStuff();  // This installs the handlers
  28. //      void RemoveAGStuff(); // This removes them
  29. //
  30. //===============================================================================
  31.  
  32. #include "AppleEvents.h"
  33. #include "GestaltEqu.h"
  34.  
  35. #include "AGValues.h"
  36.  
  37. #define kSleepMax 216000  // long sleep time (in ticks) to avoid stealing cycles
  38.                           // an app which does something on null events might
  39.                           // sleep less
  40.  
  41. Boolean gAppleEventsFlag, gQuitFlag;
  42. long gSleepVal;
  43.  
  44. #if !defined(THINK_C) && !defined(__MWERKS__)
  45.     QDGlobals  qd;
  46. #endif
  47.  
  48. //===============================================================================
  49. // Prototypes
  50. //===============================================================================
  51.  
  52. void InitAppleEventsCoreStuff(void);
  53. void DoHighLevelEvent(EventRecord * theEventRecPtr);
  54.  
  55. pascal OSErr DoAEOpenApplication(AppleEvent * theAppleEvent,
  56.                                  AppleEvent * replyAppleEvent, 
  57.                                  long refCon);
  58.                                  
  59. pascal OSErr DoAEOpenDocuments(AppleEvent * theAppleEvent,
  60.                                AppleEvent * replyAppleEvent, 
  61.                                long refCon);
  62.  
  63. pascal OSErr DoAEPrintDocuments(AppleEvent * theAppleEvent,
  64.                                 AppleEvent * replyAppleEvent, 
  65.                                 long refCon);
  66.  
  67. pascal OSErr DoAEQuitApplication(AppleEvent * theAppleEvent,
  68.                                  AppleEvent * replyAppleEvent, 
  69.                                  long refCon);
  70.                                  
  71. //===============================================================================
  72. //
  73. // Apple event handlers to be installed
  74. //
  75. //===============================================================================
  76.  
  77. pascal OSErr DoAEOpenApplication(AppleEvent * theAppleEvent,
  78.                                  AppleEvent * replyAppleEvent, 
  79.                                  long refCon)
  80. {
  81.     return noErr;
  82. }
  83.  
  84. pascal OSErr DoAEOpenDocuments(AppleEvent * theAppleEvent,
  85.                                AppleEvent * replyAppleEvent, 
  86.                                long refCon)
  87. {
  88.     return errAEEventNotHandled;
  89. }
  90.  
  91. pascal OSErr DoAEPrintDocuments(AppleEvent * theAppleEvent,
  92.                                 AppleEvent * replyAppleEvent, 
  93.                                 long refCon)
  94. {
  95.     return errAEEventNotHandled;
  96. }
  97.  
  98. pascal OSErr DoAEQuitApplication(AppleEvent * theAppleEvent,
  99.                                  AppleEvent * replyAppleEvent, 
  100.                                  long refCon)
  101. {
  102.     gQuitFlag = true;
  103.     return noErr;
  104. }
  105.  
  106. void InitAppleEventsCoreStuff(void)
  107. // install Apple event handlers
  108. {
  109.     OSErr retCode;
  110.     
  111.     if (gAppleEventsFlag) {
  112.         
  113.         retCode = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
  114.                     NewAEEventHandlerProc (DoAEOpenApplication), 0, false);
  115.                                         
  116.         if (retCode == noErr)
  117.             retCode = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
  118.                     NewAEEventHandlerProc (DoAEOpenDocuments), 0, false);
  119.  
  120.         if (retCode == noErr)
  121.             retCode = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
  122.                     NewAEEventHandlerProc (DoAEPrintDocuments), 0, false);
  123.         if (retCode == noErr)
  124.             retCode = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
  125.                     NewAEEventHandlerProc (DoAEQuitApplication), 0, false);
  126.         
  127.         if (retCode != noErr) DebugStr("\pInstall event handler failed");
  128.         // a better way to indicate an error is to post a notification                    
  129.     }
  130. }
  131.  
  132. void DoHighLevelEvent(EventRecord * theEventRecPtr)
  133. // high-level event dispatching
  134. {
  135.     (void) AEProcessAppleEvent(theEventRecPtr);
  136. }
  137.  
  138.  
  139. main()
  140. {
  141.     OSErr retCode;
  142.     long gestResponse;
  143.     
  144.     EventRecord mainEventRec;
  145.     Boolean eventFlag;
  146.     
  147.     // faceless background apps only get a 2K stack by default.  If necessary,
  148.     // increase the stack size here (by calling GetApplLimit to find the current
  149.     // heap limit, and SetApplLimit to set it to a lower address, thus reserving
  150.     // more space for the stack)
  151.     
  152.     // initialize QuickDraw globals
  153.     
  154.     InitGraf(&qd.thePort);
  155.     
  156.     // initialize application globals
  157.     
  158.     gQuitFlag = false;
  159.     gSleepVal = kSleepMax;
  160.     
  161.     // is the Apple Event Manager available?
  162.     retCode = Gestalt(gestaltAppleEventsAttr, &gestResponse);
  163.     if (retCode == noErr &&
  164.         (gestResponse & (1 << gestaltAppleEventsPresent)) != 0)
  165.         gAppleEventsFlag = true;
  166.     else gAppleEventsFlag = false;
  167.  
  168.     // install Apple event handlers
  169.     InitAppleEventsCoreStuff();
  170.     
  171. // Install Apple Guide handlers
  172.     if (InitAGStuff())
  173.         DebugStr("\pError occured insralling AG handlers");
  174.     else
  175.     {
  176.         // main event loop
  177.         
  178.         while (!gQuitFlag) {
  179.             eventFlag = WaitNextEvent(everyEvent, &mainEventRec, gSleepVal, nil);
  180.             
  181.             if (mainEventRec.what == kHighLevelEvent)
  182.                 DoHighLevelEvent(&mainEventRec);
  183.                 
  184.             // during testing, I like to call GetKeys here and check if the CapsLock
  185.             // key is down.  If it is, set gQuitFlag so the program will exit.
  186.         }
  187.  
  188. // Remove Apple Guide handlers
  189.         RemoveAGStuff();
  190.     }
  191. }
  192.  
  193.  
  194.  
  195.